var http = require('../../utils/http.js');
const app = getApp();
Component({
      /**
       * 组件的属性列表
       */
      properties: {
            item: Object,

      },

      /**
       * 组件的初始数据
       */
      data: {
            multiArray: [[], [], []],
            selectIndex:null,
      },
      ready() {

            http.request({
                  method: 'GET',
                  url: '/category/categoryInfo',
                  data: {
                        parentId: 0
                  },
                  callBack: (res) => {

                        let arr = this.data.multiArray;
                        arr[0] = res

                        this.setData({
                              multiArray: arr
                        }, () => {


                              this.getCategoryList(this.data.multiArray[0][0].categoryId, 1)

                              // this.getCategoryList(this.data.multiArray[1][0].categoryId, 2)


                        })
                  }

            })

      },
      // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
      attached: function () {
            //console.log(this.data.item);
      },
      /**
       * 组件的方法列表
       */
      methods: {
            getCategoryList(id, index, cb) {
                  http.request({
                        method: 'GET',
                        url: '/category/categoryInfo',
                        data: {
                              parentId: id
                        },
                        callBack: (res) => {
                              let arr = this.data.multiArray;
                              arr[index] = res
                              //   console.log(index,'index-----')
                              // console.log(arr[index],'arr[index]====')
                              this.setData({
                                    multiArray: arr
                              }, () => {
                                    cb && cb();
                                    if (this.data.multiArray[2].length > 0) return
                                    console.log("进来了")
                                    this.getCategoryList(this.data.multiArray[1][0].categoryId, 2)

                              })
                        }

                  })

            },

            pickerChange(e) {

                  console.log(e, 'e----')
                  let column = e.detail.column

                  let value = e.detail.value
                  console.log(column, value, 'column----')
                  if (column >= 2) return
                  // this.getCategoryList(this.data.multiArray[column][value].categoryId, column+1)
                  if (column == 0) {

                        let index = this.data.selectIndex ? this.data.selectIndex : 0
                        console.log(index, 'index----')
                        this.getCategoryList(this.data.multiArray[column][value].categoryId, 1, () => {
                              this.getCategoryList(this.data.multiArray[column + 1][index].categoryId, 2)
                        })


                  } else if (column == 1) {
                        this.setData({
                              selectIndex: value
                        })

                        this.getCategoryList(this.data.multiArray[column][value].categoryId, 2)
                  }
            },
            bindchangeValue(e) {
                  console.log(e.detail.value,'value---')
                  let selectArr = e.detail.value;
                  selectArr.forEach(key=>{
                        if(!key){
                              key=0;
                        }
                  })


                  let first = this.data.multiArray[0][selectArr[0]]
                  let second = this.data.multiArray[1][selectArr[1]]

                  let three = this.data.multiArray[2][selectArr[2]]

                  let str = first.categoryName + "-" + second.categoryName + '-' + three.categoryName

                  this.triggerEvent('getCategory', {
                        showCategoryName: str,
                        yeCategoryId: first.categoryId,
                        parentId: second.categoryId,
                        categoryId: three.categoryId,
                  });
            },
      }
})